package com.xxx.video.main.adapters;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.xxx.video.R;
import com.xxx.video.commen.adapters.BaseRecycleViewAdapter;
import com.xxx.video.commen.adapters.BaseViewHolder;
import com.xxx.video.commen.utils.CountUtils;
import com.xxx.video.commen.utils.DimensionUtil;
import com.xxx.video.commen.utils.DownloadUtils;
import com.xxx.video.commen.utils.ListUtils;
import com.xxx.video.commen.utils.ResourcesUtils;
import com.xxx.video.commen.utils.StringUtils;
import com.xxx.video.main.beans.VideoListBean;

import butterknife.BindView;

public class HomeFragmentRecommendVideoListAdapter extends BaseRecycleViewAdapter<VideoListBean, BaseViewHolder> {

    public HomeFragmentRecommendVideoListAdapter(@NonNull Context context) {
        super(context);
    }

    @Override
    public BaseViewHolder onCreateDefaultViewHolder(View view, int viewType) {
        if (viewType == 0) {
            return new MyRecommendVerticalListHolder(view);
        }
        return new MyRecommendGridListHolder(view);
    }

    @Override
    public int getItemLayout(int viewType) {
        if (viewType == 1) {
            return R.layout.item_home_fragment_recommend_video_list;
        } else {
            return R.layout.item_home_fragment_vertical_video_list;
        }
    }

    @Override
    public boolean isSetBaseItemViewClickListener() {
        return false;
    }

    @Override
    public int getItemViewType(int position) {
        VideoListBean listBean = getItem(position);
        if (ListUtils.isEmpty(listBean.list)) {
            return 0;
        } else {
            return 1;
        }
    }

    class MyRecommendVerticalListHolder extends BaseViewHolder {


        @BindView(R.id.iv_item_home_fragment_vertical_video_list_cover)
        ImageView ivItemHomeFragmentVerticalVideoListCover;
        @BindView(R.id.tv_item_home_fragment_vertical_video_list_play_count)
        TextView tvItemHomeFragmentVerticalVideoListPlayCount;
        @BindView(R.id.tv_item_home_fragment_vertical_video_list_comment_count)
        TextView tvItemHomeFragmentVerticalVideoListCommentCount;
        @BindView(R.id.tv_item_home_fragment_vertical_video_list_time)
        TextView tvItemHomeFragmentVerticalVideoListTime;
        @BindView(R.id.tv_item_home_fragment_vertical_video_list_desc)
        TextView tvItemHomeFragmentVerticalVideoListDesc;
        @BindView(R.id.tv_item_home_fragment_vertical_video_list_download_count)
        TextView tvItemHomeFragmentVerticalVideoListDownloadCount;
        @BindView(R.id.tv_item_home_fragment_vertical_video_list_type)
        TextView tvItemHomeFragmentVerticalVideoListType;
        @BindView(R.id.cl_item_home_fragment_vertical_video_list)
        ConstraintLayout clItemHomeFragmentVerticalVideoList;


        public MyRecommendVerticalListHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            VideoListBean listBean = getItem(position);
            /*
             * title
             */
            tvItemHomeFragmentVerticalVideoListDesc.setText(StringUtils.getInstance().getNotEmptyMsg(listBean.fTitle));
            /*
             * cpver
             */
            Glide.with(context).load(ResourcesUtils.getInstance().getImagePath(listBean.fPicurl))
                    .apply(new RequestOptions()
                            .error(R.drawable.bg_place_holder_nine_five)
                            .placeholder(R.drawable.bg_place_holder_nine_five)
                            .transform(new RoundedCorners(DimensionUtil.dpToPx(5))))
                    .into(ivItemHomeFragmentVerticalVideoListCover);
            /*
             * play count
             */
            tvItemHomeFragmentVerticalVideoListPlayCount.setText(CountUtils.getInstance().getCount(listBean.fPlayTimes));
            /*
             *comment count
             */
            tvItemHomeFragmentVerticalVideoListCommentCount.setText(CountUtils.getInstance().getCount(listBean.fCommentTimes));
            /*
             * download count
             */
            tvItemHomeFragmentVerticalVideoListDownloadCount.setText(CountUtils.getInstance().getCount(listBean.fDownloadTimes));
            tvItemHomeFragmentVerticalVideoListDownloadCount.setOnClickListener(v -> DownloadUtils.getInstance().startDownload(context, listBean.fVideo, listBean.fContentId,
                    listBean.fTitle, listBean.fPicurl, listBean.fCreateTime, listBean.fPlayTimes, listBean.fCommentTimes, 0));
            /*
             * time
             */
            tvItemHomeFragmentVerticalVideoListTime.setText(StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fCreateTime)));
            /*
             * type
             */
            String type = String.format("%s﹒%s", StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fTypeName)), StringUtils.getInstance().getNotEmptyMsg(String.valueOf(listBean.fLabel)));
            if (TextUtils.isEmpty(listBean.fTypeName) || TextUtils.isEmpty(listBean.fLabel)) {
                type = type.replace("﹒", "");
            }
            /*
             *label
             */
            tvItemHomeFragmentVerticalVideoListType.setText(type);
            clItemHomeFragmentVerticalVideoList.setOnClickListener(v -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(listBean, position, 0);
                }
            });
        }
    }

    class MyRecommendGridListHolder extends BaseViewHolder {

        @BindView(R.id.rcv_item_home_fragment_recommend_grid_list)
        RecyclerView rcvItemHomeFragmentRecommendGridList;

        public MyRecommendGridListHolder(View itemView) {
            super(itemView);
        }

        @Override
        public void setData2View(int position) {
            VideoListBean listBean = getItem(position);
            rcvItemHomeFragmentRecommendGridList.setLayoutManager(new GridLayoutManager(context, 2));
            HomeFragmentGridVideoListAdapter homeFragmentGridVideoListAdapter = new HomeFragmentGridVideoListAdapter(context);
            rcvItemHomeFragmentRecommendGridList.setAdapter(homeFragmentGridVideoListAdapter);
            homeFragmentGridVideoListAdapter.setData(listBean.list);
            homeFragmentGridVideoListAdapter.setOnItemClickListener((OnItemClickListener<VideoListBean>) (listBean1, position1, type) -> {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(listBean1, position1, type);
                }
            });
        }
    }
}
